package com.qss.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qs.mapper.SysRoleMapper;
import com.qs.mapper.SysRoleResourceMapper;
import com.qs.pojo.SysRole;
import com.qs.pojo.SysRoleExample;
import com.qs.pojo.SysRoleResource;
import com.qs.pojo.SysRoleResourceExample;
import com.qss.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@Service(interfaceClass = RoleService.class,timeout = 10000)
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;

    @Autowired
    private SysRoleResourceMapper Mapper;
    @Override
    public List<SysRole> queryRole(SysRole role) {
        SysRoleExample ex = new SysRoleExample();
        SysRoleExample.Criteria criteria = ex.createCriteria();
        if (role!=null){
            if (role.getStatus()!=-1){
                criteria.andStatusEqualTo(role.getStatus());
            }
            if (!StringUtils.isEmpty(role.getRoleName())){
                criteria.andRoleNameLike("%"+role.getRoleName()+"%");
            }
        }
        List<SysRole> sysRoles = mapper.selectByExample(ex);
        return sysRoles;
    }

    @Override
    public PageInfo<SysRole> queryRoleByPage(SysRole role, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysRole> sysRoles = this.queryRole(role);
        return new PageInfo<SysRole>(sysRoles);
    }

    @Override
    public SysRole queryById(Integer roleId) {
        return mapper.selectByPrimaryKey(roleId);
    }

    @Override
    public boolean savaRole(SysRole role) {
        if (role.getRoleId()!=null){
            return mapper.updateByPrimaryKeySelective(role)==1?true:false;
        }

        return mapper.insertSelective(role)==1?true:false;
    }

    @Override
    public boolean delRoles(Integer[] ids) {
        int num=0;
        for (Integer id : ids) {
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public boolean saveRolePermission(SysRole role, Integer[] ids) {
        int flag=0;
        delPermissionById(role.getRoleId());
        if (ids!=null&&ids.length>0) {
            for (Integer id : ids) {
                SysRoleResource sysRoleResource = new SysRoleResource();
                sysRoleResource.setRoleId(role.getRoleId());
                sysRoleResource.setResourceId(id);
                flag+=Mapper.insertSelective(sysRoleResource);
            }
            return flag==ids.length?true:false;
        }

        return true;
    }
    public int delPermissionById( int roleId){
        SysRoleResourceExample ex=new SysRoleResourceExample();
        SysRoleResourceExample.Criteria criteria = ex.createCriteria();
        criteria.andRoleIdEqualTo(roleId);

        return Mapper.deleteByExample(ex);
    }

    @Override
    public SysRole queryRoleByName(String roleName) {

        SysRoleExample ex=new SysRoleExample();
        SysRoleExample.Criteria criteria = ex.createCriteria();

        criteria.andRoleNameEqualTo(roleName);
        List<SysRole> sysRoles = mapper.selectByExample(ex);
        if (sysRoles==null||sysRoles.size()==0) {
            return null;

        }
        return sysRoles.get(0);
    }
}
